<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表格</title>
  <style>
    #c {
      border: 1px solid #ccc;
      box-sizing: border-box;
    }
  </style>
</head>
<body>
  <canvas id="c" width="500" height="400"></canvas>

  <script>
    const context = document.getElementById('c')
    const ctx = context.getContext('2d')

    // 定义变量，保存小方格的尺寸
    let gridSize = 20

    // 获取canvas宽高
    const canvasWidth = ctx.canvas.width
    const canvasHeight = ctx.canvas.height

    // 计算垂直方向和水平方向可以绘制多少条线
    let row = Math.floor(canvasHeight / gridSize)
    let col = Math.floor(canvasWidth / gridSize)

    // 绘制垂直方向的横线
    for (let i = 0; i < row; i++) {
      ctx.beginPath()
      ctx.moveTo(0, i * gridSize - 0.5)
      ctx.lineTo(canvasWidth, i * gridSize - 0.5)
      ctx.strokeStyle = '#ccc'
      ctx.stroke()
    }

    // 绘制水平方向的竖线
    for (let i = 0; i < col; i++) {
      ctx.beginPath()
      ctx.moveTo(i * gridSize - 0.5, 0)
      ctx.lineTo(i * gridSize - 0.5, canvasHeight)
      ctx.strokeStyle = '#ccc'
      ctx.stroke()
    }
  </script>
</body>
</html>